package com.zenchn.enums;

import java.util.ArrayList;
import java.util.List;

/***
 * 数据值类型 枚举
 * @author dingxinfa
 * @date 2018-08-13
 */
public enum DataValueStyleEnum {
    /** boolean **/
    BOOLEAN("boolean"),
    /** byte **/
    BYTE("byte"),
    /** char **/
    CHAR("char"),
    /** short **/
    SHORT("short"),
    /** int **/
    INT("int"),
    /** float **/
    FLOAT("float"),
    /** long **/
    LONG("long"),
    /** double **/
    DOUBLE("double"),
    /** json **/
    JSON("json");

    /**中文描述**/
    private String value;
    DataValueStyleEnum(String value){
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }


    /**
     * 根据value返回枚举类型
     */
    public static DataValueStyleEnum getEnum(String value) {
        for (DataValueStyleEnum code : values()) {
            String v=code.getValue();

            if (v.equals(value)) {
                return code;
            }
        }
        return null;
    }


    /**
     * 获取 枚举值列表
     */
    public static List<String> getValueList() {
        List<String> valueList=new ArrayList<>();
        for (DataValueStyleEnum code : values()) {
            valueList.add(code.getValue());
        }
        return valueList;
    }



    /**
     * 根据value返回 枚举值对应 class 类型
     */
    public static Class getValueClass(String value) {
        for (DataValueStyleEnum code : values()) {
            String v=code.getValue();
            if (v.equals(value)) {
                return getValueClass(code);
            }
        }
        return null;
    }

    /***
     * 通过枚举 获取 class 类型
     * @param code
     * @return
     */
    private static Class getValueClass(DataValueStyleEnum code){
        Class clazz=null;
        switch (code) {
            case BOOLEAN:
                clazz = (Class) Boolean.class;
                break;
            case BYTE:
                clazz = (Class) Byte.class;
                break;
            case CHAR:
                clazz = (Class) String.class;
                break;
            case SHORT:
                clazz = (Class) Short.class;
                break;
            case INT:
                clazz = (Class) Integer.class;
                break;
            case FLOAT:
                clazz = (Class) Float.class;
                break;
            case LONG:
                clazz = (Class) Long.class;
                break;
            case DOUBLE:
                clazz = (Class) Double.class;
                break;
            default:
                clazz = (Class) String.class;
        }
        return  clazz;
    }
}
